package com.ktjiaoyu.controller;

import com.ktjiaoyu.entity.SysUser;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 * @return 13141
 * 2025/6/30
 * 14:35
 */
@Controller
@RequestMapping("sub")
public class RegisterController {
    private static final Logger LOGGER = LoggerFactory.getLogger(RegisterController.class);
//    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        LOGGER.info("Spring MVC框架搭建成功，当前请求路径：{}", request.getRequestURI());
        // 返回逻辑视图名称
        return new ModelAndView("register");
    }
    /**
     * 使用注解时，控制器方法是一个普通方法
     * 由@RequestMapping注解映射请求路径
     */
    @RequestMapping("register")
    protected String hello(HttpServletRequest request) throws Exception {
        LOGGER.info("使用注解模式实现控制器，请求路径：{}", request.getRequestURI());
        // 返回字符串结果作为逻辑视图名
        return "register";
    }
    //    model 使用 Map能得到相同效果
    @RequestMapping("/model")
    public String model(Model model, @RequestParam(name = "account", required = false)
    String account, @RequestParam(name = "password") String password) throws Exception {
        LOGGER.info("恭喜{}, 注册成功！密码：{}", account, password);
        // 使用 model 对象添加数据
        model.addAttribute("account", account);
        // 创建 SysUser 对象，并存入 model对象
        SysUser sysUser = new SysUser();
        sysUser.setAccount(account);
        sysUser.setPassword(password);
        model.addAttribute("sysUser", sysUser);
        return "success";
    }
}
